const events=require('events');
const redis=require('../models/redis.server.model');

const map={
    newTopic:'发表话题',
    editTopic:'编辑话题',
    removeTopic:'删除话题',
    comment:'评论话题',
    collectTopic:'收藏话题',
    de_collectTopic:'取消收藏话题',
    getUserCollections:'获取用户所有收藏话题',
    reply:'回复评论',
    ups:'点赞',
    getUser:'获取用户',
    getMessageCount:'获取未读消息数',
    getMessages:'获取全部已读和未读消息',
    markAll:'标记全部消息',
    markOne:'标记单个消息'
};

module.exports=opts=>async (ctx,next)=>{
    let {token}=ctx.query,ee=new events;
    ee.on('invalid',msg=>{
        ctx.body={
            code:400,
            msg
        }
    });
    if(!token)return ee.emit('invalid','Token不存在');
    let ret=await redis.getLimit({action:opts.action,id:token});
    if(ret)return ee.emit('invalid',map[opts.action]+'频繁，请稍候再试');
    await next();
};